namespace cframework {
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;

public static class MD5Hash
{
    private static MD5 md5 = MD5.Create();

    public static string HashString(string sourceString)
    {
        byte[] source = md5.ComputeHash(Encoding.UTF8.GetBytes(sourceString));
        return HashBytes(source);
    }

    public static string HashBytes(byte[] data)
    {
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString();
    }

    public static string GetMD5HashFromFile(string fileName)
    {
        FileStream file = null;
        try
        {
            file = new FileStream(fileName, FileMode.Open);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
        }
        finally
        {
            if (file != null)
            {
                file.Close();
            }
        }

        //return null;
    }

    public static string GetMD5HashFromString(string str)
    {
        MemoryStream memStream = null;
        try
        {
            memStream = new MemoryStream(Encoding.UTF8.GetBytes(str));
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(memStream);
            memStream.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
        }
        finally
        {
            if (memStream != null)
            {
                memStream.Close();
            }
        }
    }

    public static string GetMD5Hash(string str)
    {
        return GetMD5Hash(Encoding.UTF8.GetBytes(str));
    }

    public static string GetMD5Hash(byte[] data)
    {
        MemoryStream memStream = new MemoryStream(data);
        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] retVal = md5.ComputeHash(memStream);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < retVal.Length; i++)
        {
            sb.Append(retVal[i].ToString("x2"));
        }
        return sb.ToString();
    }
}


}